//
//  AliPayRequest.swift
//  eat_known
//
//  Created by 心情 on 2018/8/3.
//  Copyright © 2018年 心情. All rights reserved.
//

import Foundation
import Alamofire
import SwiftSoup

//MARK:- 支付宝
class AlipayRequest: FoodRequestProtocol {
    static func start(keyword: String!, completion: @escaping (Array<Food>?, Error?) -> Void) {
        let urlStr = "http://app.jbaobao.com/index.php/V3/FoodGuide/getIngredientList?_=1532588926712"
        
        Alamofire.request(urlStr,
                          method: .post,
                          parameters: [ "keyword": keyword ],
                          encoding: JSONEncoding.default,
                          headers:nil).responseString { (response) in
                            switch response.result {
                            case .failure(let error):
                                completion(nil, error);
                            case .success(let json):
                                let foods = [Food].deserialize(from: json, designatedPath: "data.ingredient_list.list")?.compactMap{$0}
                                foods?.forEach({ (food) in
                                    food.source = .AliPay
                                })
                                completion(foods, nil)
                            }
        }
    }
}
